var companyId;
var editType;	//类型，当为add时为增加，当为edit时为编辑
var cidata;
$(function () {
    companyId = $.getUrlParam("id");
    editType = $.getUrlParam("type");
    
    //是否验证成功
    $('#branchCompanyForm').validationEngine('attach', {
        validationEventTrigger : '',	//当提交的时候验证是否合法
        promptPosition: 'topRight',
        scroll: false,
        focusFirstField : true,
        onValidationComplete : function (form, valid) {
            if (valid){
            	if (editType == 'add') {
            		addBranchCompanyInfo();
                } else {
                	modifyBranchCompanyInfo();
                }
            }
        }
    });
    
    //如果company为add，则是增加操作，否则是编辑操作.
    //通过公司的id，查询公司的信息
    if(editType == 'add'){
    	$.ajax({
            url: "../company/getCompanyInfoById.htm",
            cache: false,
            async : false,
            type: "post",
            dataType: "json",
    	    data: {companyId: companyId},
            success: function (result) {
                if (result.status == 0) {
                	cidata = result.data;
                    return;
                }
            }
        });
    	return;
    }else{
    	echoCompanyInfo();
    }
});

//点击编辑回显数据
function echoCompanyInfo(){
    $.ajax({
        url: "../company/getCompanyInfoById.htm",
        cache: false,
        async : false,
        type: "post",
        dataType: "json",
	    data: {companyId: companyId},
        success: function (result) {
            if (result.status == 0) {
                var data = result.data;
                $("#branchCompanyForm").formatForm(data);
                return;
            }
        }
    });
}

//关闭窗口
function closeForm() {
    var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
    parent.layer.close(index);
}

//修改
function modifyBranchCompanyInfo(){
    $.ajax({
        url: "../company/modifyCompanyInfo.htm",
        cache: false,
        type: "post",
        dataType: "json",
        data : $("#branchCompanyForm").serialize(),
        beforeSend:function(){var index = layer.load(1, { shade: [0.8,'#ddd']});},
        complete:function(){var index = layer.closeAll("loading");},
        success: function (result) {
            if (result.status == 0) {
                parent.layer.msg(result.message, {icon: 1});
                parent.$table.bootstrapTable('refresh', {url: '../company/getBranchCompanyInfo.htm?companyId='+ $('#comnumParentId').val()});
                closeForm();
            } else {
                parent.layer.msg(result.message, {icon: 2});
            }
        }
    });
}

//增加
function addBranchCompanyInfo() {
	var dataParam = $("#branchCompanyForm").serialize();
	dataParam = dataParam + "&comnumTfn=" + cidata.comnumTfn + "&comnumLicenceCode=" + cidata.comnumLicenceCode + "&comnumRegisterTime=" + cidata.comnumRegisterTime
				+ "&comnumIdCard=" + cidata.comnumIdCard + "&comnumBankName="+ cidata.comnumBankName + "&comnumBankHolder=" + cidata.comnumBankHolder
				+ "&comnumBankCard=" + cidata.comnumBankCard + "&comnumMerchantNo=" + cidata.comnumMerchantNo + "&comnumKey=" + cidata.comnumKey
				+ "&comnumIdCardImgName=" + cidata.comnumIdCardImgName + "&comnumDoorHeaderImgName=" + cidata.comnumDoorHeaderImgName
				+ "&comnumCorp=" + cidata.comnumCorp + "&comnumRealName=" + cidata.comnumRealName + "&comnumParentId=" + companyId
    $.ajax({
        url: "../company/addToBranchCompanyInfo.htm",
        cache: false,
        type: "post",
        dataType: "json",
        data: dataParam,
        beforeSend:function(){var index = layer.load(1, { shade: [0.8,'#ddd']});},
        complete:function(){var index = layer.closeAll("loading");},
        success: function (result) {
            if (result.status == 0) {
                parent.layer.msg(result.message, {icon: 1});
                parent.$table.bootstrapTable('refresh', {url: '../company/getBranchCompanyInfo.htm?companyId='+ companyId});
                closeForm();
            } else {
                parent.layer.msg(result.message, {icon: 2});
            }
        }
    });
}
